package com.credit.web.basic;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springside.modules.persistence.SearchDataTransform;

import com.credit.context.CreditServletContext;
import com.credit.entity.CreditCoinRecord;
import com.credit.entity.CreditPointsRecord;
import com.credit.entity.CreditUserInfo;
import com.credit.service.CreditCoinService;
import com.credit.util.BeanToMapUtil;
import com.credit.util.ParamsConstant;
import com.credit.web.basic.bean.ResBean;

@Controller
@RequestMapping("/coin")
public class CoinController {
	@Autowired
	private CreditCoinService creditCoinService;
	
	@ResponseBody
	@RequestMapping(value = "/mylist", method = RequestMethod.POST)
	public void mylist(HttpServletRequest request, HttpServletResponse response,
			@RequestParam(value = "page",defaultValue = "1") int pageNumber) throws IllegalArgumentException, IllegalAccessException {
		CreditUserInfo userInfo = CreditServletContext.getUserInfo();
		
		Map<String, Object> searchParams = new TreeMap<String, Object>();
		searchParams.put("EQ_userId", userInfo.getId());
		
		Map<String, SearchDataTransform> dataTyps = new HashMap<String, SearchDataTransform>();
		

		Page<CreditCoinRecord> page = creditCoinService.getCoinListPage(searchParams, pageNumber, ParamsConstant.PAGESIZE, "createTime", dataTyps);
		List<CreditCoinRecord> coinRecordList = page.getContent();
		List<Map<String,Object>> coinRecordListReturn = new ArrayList<Map<String,Object>>();
		for(CreditCoinRecord coinRecord:coinRecordList){
			coinRecordListReturn.add(BeanToMapUtil.getMap(coinRecord));
		}
		
		ResBean resBean = CreditServletContext.getResBean();
		resBean.getData().put("coinRecordList", coinRecordListReturn);
		CreditServletContext.setResBean(resBean);
	}
	
	@ResponseBody
	@RequestMapping(value = "/list")
	public void list() throws IllegalArgumentException, IllegalAccessException {
		
		List<Map<String,Integer>> returnList = new ArrayList<Map<String,Integer>>();
		Map<Integer,Integer> coinMap = ParamsConstant.COIN_LIST_MAP;
		Map<Integer,Integer> coinPointsMap = ParamsConstant.COIN_POINTS_MAP;
		for(Map.Entry<Integer, Integer> entity:coinMap.entrySet()){
			Map<String,Integer> returnMap = new HashMap<String,Integer>();
			returnMap.put("coins", entity.getKey());
			returnMap.put("money", entity.getValue());
			returnMap.put("points", coinPointsMap.get(entity.getKey()));
			returnList.add(returnMap);
		}
		
		ResBean resBean = CreditServletContext.getResBean();
		resBean.getData().put("coinList", returnList);
		CreditServletContext.setResBean(resBean);
	}

}
